<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-7">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <link rel="stylesheet" href="./font/iconfont.css">
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .da{ 
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
            top: 150px;
            left: 0;
            margin: auto;
        }
        ul{
            display: flex;
            position: relative;
            /* transition: all .5s; */
        }
        li{
            width: 400px;
            height: 400px;
            list-style: none;
            background-color: aqua;
        }
        li img{
            width: 400px;
            height: 400px;
        }
        .da span{
            display: none;
            position: absolute;
            top: 50%;
            font-size: 40px;
            margin-top: -20px;
        }
        .da .l{
            left: 10px;
        }
        .da .r{
            right: 10px;
        }
        ol{
            position: absolute;
            left: 50%;
            bottom: 30px;
            display: flex;
            margin-left: -50px;
        }
        ol li{
            width: 10px;
            height: 10px;
            background-color: #fff;
            margin-left: 10px;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="da">
        <ul>
            <li><img src="./images/1.jpg"></li>
            <li><img src="./images/2.jpg"></li>
            <li><img src="./images/3.jpg"></li>
            <li><img src="./images/4.jpg"></li>
            <li><img src="./images/5.jpg"></li>
        </ul>
        <span class="iconfont icon-xiangyoujiantou r"></span>
        <span class="iconfont icon-xiangzuojiantou l"></span>
        <ol>
            <li style="background-color: red;"></li>
            <li></li>
            <li></li>
            <li></li>
            <li ></li>
        </ol>
    </div>
</body>
</html>
<script>
    var da=document.querySelector('.da')
    var imgs=document.querySelectorAll('ul img')
    var ul=document.querySelector('ul')
    var lis=document.querySelectorAll('ol li')
    var r=document.querySelector('.r')
    var l=document.querySelector('.l')
    var num=0//当前图片的索引
    var yuan=0//小圆圈的索引
    var count=imgs.length//不包括克隆的真实图片数量
    //克隆第一张图到末尾实现无缝滚动
    var tuclone=ul.children[0].cloneNode(true)
    ul.appendChild(tuclone)

    r.onclick=function(){
        num++
        if(num>count){//到达最后一张真实图片后
            ul.style.transition='none'//禁用过渡效果
            ul.style.transform=`translateX(0px) `//回到起始位置
            num=1//设置为第二张图片的索引
            setTimeout(()=>{//延迟执行以确保样式更新
               ul.style.transition='all .5s'
               ul.style.transform=`translateX(${-num*400}px) `
            },16)//一个动画帧的时间为16ms
        }else{
            ul.style.transition='all .5s'
            ul.style.transform=`translateX(${-num*400}px) ` 
        }
        yuan=(yuan+1)%lis.length
        bs(yuan)
    }
    l.onclick=function(){
        if(num==0){//如果是第一张图片
            //禁用过渡效果
            ul.style.transition='none'
            num=count;//设置为最后一张真实图片的索引
            ul.style.transform=`translateX(${-num*400}px) `
            setTimeout(()=>{//延迟执行以确保样式更新
                num--
                ul.style.transition='all .5s'
                ul.style.transform=`translateX(${-num*400}px) `
            },16)//一个动画帧的时间为16ms
        }else{
            num--
            ul.style.transition='all .5s'
            ul.style.transform=`translateX(${-num*400}px) ` 
        }
        yuan=(yuan-1+lis.length)%lis.length
        bs(yuan)
    }
    da.onmouseenter=function(){
        r.style.display='block'
        l.style.display='block'
        clearInterval(dsq)
    }
    da.onmouseleave=function(){
        r.style.display='none'
        l.style.display='none'
        lb()
    }
    function lb(){
        dsq=setInterval(function(){
            r.onclick()
        },2000)
    }
    lb()
    for(i=0;i<lis.length;i++){
        lis[i].setAttribute('index',i)
        lis[i].onclick=function(){
            var index=this.getAttribute('index')
            ul.style.transform=`translateX(${-index*400}px) `
            bs(index)
        }
    }
    function bs(index){
        for(i=0;i<lis.length;i++){
            lis[i].style.backgroundColor='#fff';
        }
        lis[index].style.backgroundColor='red';
    }
</script>